<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1 class="m-0 text-dark">客户管理</h1>
            </div><!-- /.col -->
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a th:href="@{admin}">首页</a></li>
                    <li class="breadcrumb-item active">权限管理</li>
                    <li class="breadcrumb-item active">客户管理</li>
                </ol>
            </div><!-- /.col -->
        </div><!-- /.row -->
    </div><!-- /.container-fluid -->
</div>
<!-- Main content -->
<section class="content">
    <div class="card card-info">
        <shiro:hasPermission name="customer:list">
            <form id="searchForm" class="needs-validation form-inline" novalidate="">
                <div class="form-group">
                    <label for="username">用户名:</label>
                    <input type="text" class="form-control m-2" id="username">
                </div>
                <div class="form-group">
                    <label for="nicname">姓名:</label>
                    <input type="text" class="form-control m-2" id="nicname">
                </div>
                <div class="form-group">
                    <label for="companyName">公司名称:</label>
                    <input type="text" class="form-control m-2" id="companyName">
                </div>
                <div class="form-group">
                    <label for="type">用户类型:</label>
                    <select id="type" class="form-control" name="type">
                        <option value="">请选择</option>
                        <option value="0">个人用户</option>
                        <option value="1">企业用户</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="type">会员类型:</label>
                    <select id="vipType" class="form-control" name="vipType">
                        <option value="">请选择</option>
                        <option value="0">个人会员</option>
                        <option value="1">企业会员</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="state">用户状态:</label>
                    <select id="state" class="form-control" name="state">
                        <option value="">请选择</option>
                        <option value="0">禁用</option>
                        <option value="1">启用</option>
                    </select>
                </div>
                <div class="form-group">
                    <button class="btn btn-primary ml-3" type="submit">查询</button>
                    <button class="btn btn-default ml-2" type="reset">重置</button>
                </div>
            </form>
        </shiro:hasPermission>
    </div>
    <div class="card card-body">
        <div id="toolbar" class="btn-group">
            <shiro:hasPermission name="customer:update">
                <button id="btn_batch_delete" type="button" class="btn btn-danger">
                    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>批量禁用
                </button>
                &nbsp;&nbsp;
                <button id="btn_batch_enable" type="button" class="btn btn-info">
                    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>批量启用
                </button>
            </shiro:hasPermission>
        </div>
        <table class="table-sm" id="table"></table>
    </div>
</section>

<script>

    $(function () {

        var deleteFlag = Core.parseBool("[[${@perms.hasPerm('customer:update')}]]");

        window.operateEvents = {
            // 点击启用
            'click .btn_del': function (e, value, row, index) {
                enableRow(row.id)
            },
            // 点击禁用
            'click .btn_enable': function (e, value, row, index) {
                deleteRow(row.id)
            }
        }
        var columns = [
            {checkbox: true},
            {
                field: 'username',
                title: '用户名',
                align: "center"
            }, {
                field: 'nicname',
                title: '姓名',
                align: "center"
            }, {
                field: 'balance',
                title: '余额',
                align: "center"
            }, {
                field: 'companyName',
                title: '公司名称',
                align: "center"
            }, {
                field: 'type',
                title: '用户类型',
                align: "center",
                formatter: function (value, row, index) {
                    return value == 1 ? '企业用户' : '个人用户';
                }
            },
            {
                field: 'vipType',
                title: '会员类型',
                align: "center",
                formatter: function (value, row, index) {
                    return value == 1 ? '企业会员' : '个人会员';
                }
            },
            {
                field: 'state',
                title: '用户状态',
                align: "center",
                formatter: function (value, row, index) {
                    return value == 0 ? '禁用' : '启用';
                }
            },
            {
                field: 'operation',
                title: '操作',
                align: "center",
                width: 200,
                events: operateEvents,
                formatter: operationFormatter
            }];

        function operationFormatter(value, row, index) {
            var del = deleteFlag ? (row.state == 0 ? '<button class="btn btn-info btn-sm ml-1 btn_del">启用</button>'
                : '<button class="btn btn-danger btn-sm ml-1 btn_enable">禁用</button>') : '';
            return del;
        }

        var options = {
            id: "#table",
            url: '/customer/list',
            columns: columns,
            toolbar: '#toolbar',
            showRefresh: true,
            queryParams: queryParams
        }
        Core.initTable(options);

        function refreshTable() {
            Core.refreshTable("#table")
        }

        // 查询参数
        function queryParams(params) {
            return {
                pageNumber: params.pageNumber,
                pageSize: params.pageSize,
                username: $("#username").val(),
                nicname: $("#nicname").val(),
                companyName: $("#companyName").val(),
                type: $("#type").val(),
                state: $('#state').val(),
                vipType: $('#vipType').val()
            };
        }

        // 查询
        $('#searchForm').submit(function (event) {
            event.preventDefault();
            event.stopPropagation();
            refreshTable();
        })

        function deleteRow(id) {
            Core.confirm("确定禁用该用户吗？", function () {
                Core.postAjax("/customer/update", {"id": id, 'state': 0}, function (data) {
                    if (data.status === 200) {
                        refreshTable();
                    }
                    layer.msg(data.msg);
                })
            })
        }

        function enableRow(id) {
            Core.confirm("确定启用该用户吗？", function () {
                Core.postAjax("/customer/update", {"id": id, 'state': 1}, function (data) {
                    if (data.status === 200) {
                        refreshTable();
                    }
                    layer.msg(data.msg);
                })
            })
        }

        // 批量启用
        $("#btn_batch_enable").on('click', function () {
            var checkedRows = Core.selectMutiData("#table");
            if (checkedRows) {
                Core.confirm("确定启用选中的" + checkedRows.length + "条客户吗？", function () {
                    var ids = [];
                    $.each(checkedRows, function (i, item) {
                        ids.push(item.id);
                    })
                    Core.postAjax("/customer/batch/update", {"ids": ids, 'state': 1}, function (data) {
                        if (data.status === 200) {
                            refreshTable();
                        }
                        layer.msg(data.msg);
                    })
                })
            }
        })

        // 批量禁用
        $("#btn_batch_delete").on('click', function () {
            var checkedRows = Core.selectMutiData("#table");
            if (checkedRows) {
                Core.confirm("确定禁用选中的" + checkedRows.length + "条客户吗？", function () {
                    var ids = [];
                    $.each(checkedRows, function (i, item) {
                        ids.push(item.id);
                    })
                    Core.postAjax("/customer/batch/update", {"ids": ids, 'state': 0}, function (data) {
                        if (data.status === 200) {
                            refreshTable();
                        }
                        layer.msg(data.msg);
                    })
                })
            }
        })
    });

</script>